package org.jeecg.modules.other;

import io.swagger.annotations.Api;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.system.mapper.SysUserRoleMapper;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * token获取权限
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-12-08 9:25
 */
@Validated
@RestController
@RequestMapping("/token/api")
@Api(tags = "专业技能")
public class RoleAndPermissionTokenController {

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 获取用户角色
     * @param token
     * @return
     */
    @RequestMapping("/getRole")
    public Result<?> getRole(String token){
        String username = JwtUtil.getUsername(token);
        if(username==null){
            return Result.error("token非法无效！");
        }
        // 设置用户拥有的角色集合，比如“admin,test”
        Set<String> roleSet = sysUserService.getUserRolesSet(username);
        return Result.ok(roleSet);
    }

    /**
     * 获取用户权限
     * @param token
     * @return
     */
    @RequestMapping("/getPermission")
    public Result<?> getPermission(String token){
        String username = JwtUtil.getUsername(token);
        if(username==null){
            return Result.error("token非法无效！");
        }
        // 设置用户拥有的权限集合，比如“sys:role:add,sys:user:add”
        Set<String> permissionSet = sysUserService.getUserPermissionsSet(username);
        return Result.ok(permissionSet);
    }

    /**
     * 获取用户的url菜单和权限
     * url：/sys/permission/getUserPermissionByToken
     * 参数：token
     * 方法所在类：system/controller/SysPermissionController
     * @param token
     * @return
     */


}
